package utility;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class GenerateInput {
	
	private final static String fasz = "fassz";
	private PrintWriter writer;
	private int maxEpuletszam; 
	private Random rand;
	private String fileName;
	private String relationFileName;

	public GenerateInput(String fileName,String relationFileName){
		this.fileName = fileName;
		this.rand = new Random();
		this.relationFileName = relationFileName;
		this.maxEpuletszam = 200;
	}
	
	public GenerateInput(String fileName,String relationFileName, int seed){
		this.fileName = fileName;
		this.rand = new Random();
		this.rand.setSeed(seed);
		this.relationFileName = relationFileName;
		this.maxEpuletszam = 200;
	}
	
	public GenerateInput(String fileName,String relationFileName, int seed, int maxEpuletszam){
		this.fileName = fileName;
		this.rand = new Random();
		//this.rand.setSeed(seed);
		this.relationFileName = relationFileName;
		this.maxEpuletszam = maxEpuletszam;
	}
	
	public boolean generateBuildings() throws FileNotFoundException, UnsupportedEncodingException {
		this.writer = new PrintWriter(this.fileName, "UTF-8");
		int epuletszam = rand.nextInt(maxEpuletszam) + 1;
		System.err.println(epuletszam);
		writer.println(epuletszam);
		
		for(int i = 0; i < epuletszam; i++){
			int szelesseg = rand.nextInt(20) + 1;
			int hosszusag = rand.nextInt(20) + 1;
			int magassag = rand.nextInt(20) + 1;
			writer.println(i+":"+szelesseg+" "+hosszusag+" "+magassag);
		}
		writer.close();
		System.out.println("Input Building Generate Done");
		return generateRelations(epuletszam);
	}
	
	public boolean generateRelations(int epuletszam) throws FileNotFoundException, UnsupportedEncodingException {
		this.writer = new PrintWriter(this.relationFileName, "UTF-8");
		int kapcsolatSzam = 0;
		if(epuletszam > 50){
			kapcsolatSzam = (int) epuletszam / 8;
		} else {
			kapcsolatSzam = (int) epuletszam / 4;
		}
		
		for(int i = 0; i < kapcsolatSzam; i++){
			int elemszam =  rand.nextInt(epuletszam) + 2;
			if(elemszam > epuletszam){
				elemszam = (int) elemszam / 2;
			}
			
			writer.print(elemszam+":");
			Set<Integer> relation = new HashSet<Integer>();
			for(int j = 0; j < elemszam; j++){
				int aktRelation = -1;
				do{
					aktRelation = rand.nextInt(epuletszam);
				}while(relation.contains(aktRelation));
				relation.add(aktRelation);
			}

			for(Integer integer : relation){
				writer.print(integer+" ");
			}
			writer.print("\n");
		}
		writer.close();
		System.out.println("Relation Input Generate Done");
		return true;
	}

}
